package com.achobeta.recruitment.utils;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 马拉圈
 * Date: 2024-01-12
 * Time: 11:36
 */
@Component
public class URLUtils {

    private static String APIKEY;

    @Value("${short.url.apikey}")
    public void setAPIKEY(String apikey) {
        APIKEY = apikey;
    }


    public static String getShortURL(String longString) {
        System.out.println(APIKEY);
        String result = HttpUtils.doPost("https://openapi.aifabu.com/v1/chain/createChain", new HashMap<String, Object>() {{
            this.put("apikey", APIKEY);
            this.put("target_url", longString);
        }});
        Map<String, Object> resultMap = JsonUtils.jsonToMap(result);
        System.out.println(resultMap);
        return (String) resultMap.get("render_url");
    }

    public static int createGroup(String groupName) {
        String result = HttpUtils.doPost("https://openapi.aifabu.com/v1/chainGroup/createChainGroup", new HashMap<String, Object>() {{
            this.put("apikey", APIKEY);
            this.put("name", groupName);
        }});
        Map<String, Object> resultMap = JsonUtils.jsonToMap(result);
        System.out.println(resultMap);
        return (int) resultMap.get("group_id");
    }


}
